package com.hongdee.crm.carConfig.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.crm.carConfig.entity.TCrmCarConf;
import com.hongdee.crm.carConfig.service.ITcrmCarConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/5/11.
 */
@RestController
@RequestMapping("/crm/car/conf")
public class TCrmCarConfController {

    @Autowired
    private ITcrmCarConfService confService;


    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "roleId", defaultValue = "")String roleId,
                             @RequestParam(name = "userId", defaultValue = "")String userId,
                             @RequestParam(name = "start", defaultValue = "")String start,
                             @RequestParam(name = "limit", defaultValue = "")String limit) throws Exception {
        Map map=confService.page(roleId,userId,start,limit,uid);
        map.put("success",true);
        return map;
    }

    /**
     * 保存车辆分配规则
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmCarConf data) throws Exception {
        ResultEntity res=new ResultEntity();
        confService.saveCarConf(data,uid);
        res.setSuccess(true);
        res.setMsg("车辆分配规则保存成功!!");
        return res;
    }

    /**
     * 保存车辆分配规则
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/edit")
    @ResponseBody
    public ResultEntity edit(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmCarConf data) throws Exception {
        ResultEntity res=new ResultEntity();
        confService.saveCarConf(data,uid);
        res.setSuccess(true);
        res.setMsg("车辆分配规则保存成功!!");
        return res;
    }

    /**
     * 获得车辆分配规则详细信息
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res=new ResultEntity();
        TCrmCarConf carConf=confService.getCarConfigById(id,uid);
        res.setData(carConf);
        res.setSuccess(true);
        res.setMsg("详细查询成功");
        return null;
    }

    /**
     * 车辆档案分配-分配
     * 根据车辆分配规则，进行车辆分配
     * @param uid
     * @param token
     * @param id
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/rest")
    @ResponseBody
    public ResultEntity rest(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id,
                             @RequestParam(name = "type", defaultValue = "")String type) throws Exception {
        ResultEntity res=new ResultEntity();
        confService.distCarByCarConf(id,uid,null,type);
        res.setSuccess(true);
        res.setMsg("车辆分配完成！！");
        return  res;
    }

    /**
     * 一键重新全部分配
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/rest/all")
    @ResponseBody
    public ResultEntity restAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        ResultEntity res=new ResultEntity();
        confService.distCarByAllCarConf(uid);
        res.setSuccess(true);
        res.setMsg("车辆分配完成！！");
        return  res;
    }

    @RequestMapping("/restVin")
    @ResponseBody
    public ResultEntity restVin(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id,
                             @RequestParam(name = "upExecl", defaultValue = "upExecl") MultipartFile file,
                             @RequestParam(name = "type", defaultValue = "")String type
    ) throws Exception {
        ResultEntity res=new ResultEntity();
        confService.distCarByCarConf(id,uid,file,type);
        res.setSuccess(true);
        res.setMsg("车辆分配完成！！");
        return  res;
    }

    /**
     * 车辆分配查询
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param roleId
     * @param userId
     * @param type
     * @param plateNum
     * @param vin
     * @return
     * @throws Exception
     */
    @RequestMapping("/carPage")
    @ResponseBody
    public Map carPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestParam(name = "roleId", defaultValue = "") String roleId,
                       @RequestParam(name = "userId", defaultValue = "") String userId,
                       @RequestParam(name = "type", defaultValue = "") String type,
                       @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                       @RequestParam(name = "vin", defaultValue = "")String vin) throws Exception {
        if("".equals(roleId)){
            throw new Exception("请选择角色后使用查询功能！！");
        }
        Map map=confService.carPage(start,limit,roleId,userId,type,plateNum,vin,uid);
        map.put("success",true);
        return map;
    }

    /**
     * 车辆二次分配，对已分配或未分配的车辆进行重新分配
     * @param uid
     * @param token
     * @param roleId
     * @param userId
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/carReset")
    @ResponseBody
    public ResultEntity carReset(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "roleId", defaultValue = "") String roleId,
                                 @RequestParam(name = "userId", defaultValue = "") String userId,
                                 @RequestBody List<Map> data) throws Exception {
        ResultEntity res=new ResultEntity();
        confService.carReset(data,roleId,uid,userId);
        res.setMsg("车辆分配成功！！");
        res.setSuccess(true);
        return res;
    }


    /**
     * 删除车辆分配信息
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/del")
    @ResponseBody
    public ResultEntity del(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res=new ResultEntity();
        confService.removeCarConf(id,uid);
        res.setSuccess(true);
        res.setMsg("删除车辆资源成功！！");
        return  res;
    }

    /**
     * 清空指定人员的车辆资源分配信息
     * @param uid
     * @param token
     * @param data
     * @return
     */
    @RequestMapping("/empty")
    @ResponseBody
    public ResultEntity empty(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestBody List<Map> data) throws Exception {
        ResultEntity res=new ResultEntity();
        confService.emptyCarConfDetailByUid(uid,data);
        res.setSuccess(true);
        res.setMsg("车辆资源清除成功！！");
        return res;
    }

}
